package com.newsnetwork.user.controller;

import com.newsnetwork.api.BaseController;
import com.newsnetwork.api.controller.user.MyFansControllerApi;
import com.newsnetwork.enums.Sex;
import com.newsnetwork.grace.result.CommonJSONResult;
import com.newsnetwork.grace.result.ResponseStatusEnum;
import com.newsnetwork.pojo.vo.FansCountsVO;
import com.newsnetwork.pojo.vo.RegionRatioVO;
import com.newsnetwork.user.service.MyFansService;
import com.newsnetwork.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: HuangDekai
 * @date: 2022/11/15 14:07
 * @version: 1.0
 * @since: jdk8
 * @description: 粉丝管理相关
 */
@RestController
public class MyFansController extends BaseController implements MyFansControllerApi {
    private static final Logger log = LoggerFactory.getLogger(MyFansControllerApi.class);

    @Autowired
    private MyFansService myFansService;

    @Override
    public CommonJSONResult isMeFollowThisWriter(String writerId, String fanId) {
        if (StringUtils.isBlank(writerId)) {
            return CommonJSONResult.errorCustom(ResponseStatusEnum.FANS_WRITER_NULL_ERROR);
        }
        if (StringUtils.isBlank(fanId)) {
            return CommonJSONResult.errorCustom(ResponseStatusEnum.FANS_FANS_NULL_ERROR);
        }
        boolean res = myFansService.isMeFollowThisWriter(writerId, fanId);
        return CommonJSONResult.ok(res);
    }

    @Override
    public CommonJSONResult follow(String writerId, String fanId) {
        myFansService.follow(writerId, fanId);
        return CommonJSONResult.ok();
    }

    @Override
    public CommonJSONResult unfollow(String writerId, String fanId) {
        myFansService.unfollow(writerId, fanId);
        return CommonJSONResult.ok();
    }

    @Override
    public CommonJSONResult queryAll(String writerId, Integer page, Integer pageSize) {
        if (StringUtils.isBlank(writerId)) {
            return CommonJSONResult.errorCustom(ResponseStatusEnum.FANS_WRITER_NULL_ERROR);
        }
        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult result = myFansService.queryMyFansList(writerId, page, pageSize);
        return CommonJSONResult.ok(result);
    }

    @Override
    public CommonJSONResult queryRatio(String writerId) {
        Integer manCounts = myFansService.queryFansCounts(writerId, Sex.man);
        Integer womanCounts = myFansService.queryFansCounts(writerId, Sex.woman);

        FansCountsVO fansCountsVO = new FansCountsVO();
        fansCountsVO.setManCounts(manCounts);
        fansCountsVO.setWomanCounts(womanCounts);

        return CommonJSONResult.ok(fansCountsVO);
    }

    @Override
    public CommonJSONResult queryRatioByRegion(String writerId) {
        List<RegionRatioVO> list = myFansService.queryFansCountsByRegion(writerId);
        return CommonJSONResult.ok(list);
    }
}
